package com.cake.liulanxiangzhu.controller;

import com.cake.liulanxiangzhu.pojo.dto.CartDTO;
import com.cake.liulanxiangzhu.pojo.dto.OmsOrderSearchDTO;
import com.cake.liulanxiangzhu.pojo.entity.OmsOrder;
import com.cake.liulanxiangzhu.pojo.entity.OmsOrderItem;
import com.cake.liulanxiangzhu.pojo.entity.Pay;
import com.cake.liulanxiangzhu.pojo.vo.OmsOrderStateVO;
import com.cake.liulanxiangzhu.pojo.vo.OmsOrderVO;
import com.cake.liulanxiangzhu.service.OmsOrderService;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.JsonResult;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * 订单信息表(OmsOrder)表控制层
 *
 * @author dinghao
 * @since 2023-02-23 14:54:20
 */
@RestController
@RequestMapping("/omsOrder")
@Validated
public class OmsOrderController {
    /**
     * 服务对象
     */
    @Autowired
    private OmsOrderService omsOrderService;

    /**
     * 分页查询
     *
     * @param omsOrder 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryOmsOrderListPaging")
    public JsonResult<JsonPage<OmsOrderVO>> queryByPage(HttpServletRequest request, @RequestBody OmsOrder omsOrder, Integer page, Integer pageSize) {

        return JsonResult.ok(this.omsOrderService.queryByPage(request, omsOrder, page, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOmsOrderById")
    public JsonResult<OmsOrderVO> queryById(String id) {
        return JsonResult.ok(this.omsOrderService.queryById(id));
    }

    /**
     * 结算数据
     *
     * @param cartDTO 实体
     * @return 新增结果
     */
    @PostMapping("/insertOmsOrder")
    public JsonResult<Pay> add(HttpServletRequest request, @RequestBody CartDTO cartDTO) {
        System.out.println("request = " + request + ", cartDTO = " + cartDTO);
        Pay vo = this.omsOrderService.insert(request, cartDTO);
        return JsonResult.ok(vo);
    }

    /**
     * 购物车批量新增数据
     *
     * @param cartDTO 实体
     * @return 新增结果
     */
    @PostMapping("/insertCartOmsOrder")
    public JsonResult<Integer> insertCartOmsOrder(HttpServletRequest request,@Valid @RequestBody CartDTO cartDTO) throws ParseException {
        int i = this.omsOrderService.insertCartBatch(request, cartDTO);
        return JsonResult.ok(i);
    }


    /**
     * 立即支付-使用余额
     *
     * @param orderId 实体
     * @return 编辑结果
     */
    @PostMapping("/updateOmsOrder")
    public JsonResult<Void> edit(Integer id, String gmtAppointment, Integer orderId ) {
        this.omsOrderService.update(id, gmtAppointment, orderId);
        return JsonResult.ok();
    }

    /**
     * 删除数据
     *
     * @param orderId 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteOmsOrder")
    public JsonResult<Void> deleteById( String orderId) {
       this.omsOrderService.deleteById(orderId);
       return JsonResult.ok();
    }

    /**
     * 后台批量删除订单
     *
     * @param orderIds 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteOmsOrderBatch")
    public JsonResult<Void> deleteOmsOrderBatch(@RequestParam("orderIds") List<Integer> orderIds) {
        System.out.println("orderIds = " + orderIds);
        this.omsOrderService.deleteOrderIds(orderIds);
        return JsonResult.ok();
    }

    @PostMapping("/queryAllOmsOrder")
    public JsonResult<JsonPage<OmsOrderVO>> queryAllOmsOrder(OmsOrderSearchDTO omsOrderSearchDTO){
        System.out.println("omsOrderSearchDTO = " + omsOrderSearchDTO);
        JsonPage<OmsOrderVO> vos = omsOrderService.queryAllOrder(omsOrderSearchDTO);
        return JsonResult.ok(vos);
    }

    @PostMapping("/updateOmsOrderState")
    public JsonResult<Void> updateOmsOrderState(Integer orderId, Integer state) {
        omsOrderService.updateState(orderId,state);
        return JsonResult.ok();
    }

    @GetMapping("/queryOmsOrderState")
    public JsonResult<List<OmsOrderStateVO>> queryAllOmsOrder(){
        List<OmsOrderStateVO> vos = omsOrderService.stateVos();
        return JsonResult.ok(vos);
    }


}

